package com.example.demo.cim.client.service.impl;

import com.example.demo.cim.client.config.AppConfiguration;
import com.example.demo.cim.client.service.MsgHandle;
import com.example.demo.cim.client.service.RouteRequest;
import com.example.demo.cim.client.vo.req.GroupReqVO;
import com.example.demo.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2019-01-23
 **/
@Service
public class MsgHandler implements MsgHandle {
    private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class);


    @Autowired
    private AppConfiguration appConfiguration;

    @Autowired
    private RouteRequest routeRequest;




    @Override
    public boolean checkMsg(String msg) {
        if(StringUtil.isEmpty(msg)) {
            LOGGER.warn("不能发送空消息!");
            return true;
        }
        return false;
    }

    @Override
    public boolean innerCommand(String msg) {
        return false;
    }

    @Override
    public void sendMsg(String msg) {
//        if (aiModel) {
//            aiChat(msg);
//        } else {
//            normalChat(msg);
//        }
        normalChat(msg);
    }

    /**
     * 正常聊天
     * @param msg
     */
    private void normalChat(String msg) {
        String[] totalMsg = msg.split(";;");
        if (totalMsg.length >1) {
            //私聊 TODO
        } else {
            //群聊
            GroupReqVO groupReqVO = new GroupReqVO(appConfiguration.getUserId(), msg);
            try {
                groupChat(groupReqVO);
            } catch (Exception e) {
                LOGGER.error("Exception",e);
            }
        }
    }

    @Override
    public void groupChat(GroupReqVO groupReqVO) throws Exception {
        routeRequest.sendGroupMsg(groupReqVO);
    }
}




//
//            P2PReqVO p2PReqVO = new P2PReqVO();
//            p2PReqVO.setUserId(configuration.getUserId());
//            p2PReqVO.setReceiveUserId(Long.parseLong(totalMsg[0]));
//            p2PReqVO.setMsg(totalMsg[1]);
//            try {
//                p2pChat(p2PReqVO);
//            } catch (Exception e) {
//                LOGGER.error("Exception",e);
//            }
